import http from '../../apis/api.js';
import util from '../../utils/util.js';
import config from '../../config.js';
const app = getApp();
let self;
let timeName;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    landlordInfo:null,
    title:'',
    imgSrc:"/images/paseBg.jpg",
    landlordForm:{
      accept:false,
      file:[],
      id:'',
      ownLicNum:'',
      ownName:'',
      ownNo:'',
      ownTel:'',
      code:''
    },
    error:{
      accept:'',
      file: '',
      ownLicNum:'',
      ownName:'',
      ownTel:'',
      code:''
    },
    code:{
      disabled:false,
      text:'获取验证码'
    },
    times:60,
    examine:''
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self=this;
    self.data.examine = options.examine ? options.examine:'';
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        http.langlordLogin({ openId: res.data }).then(result => {
          wx.setStorage({
            key: 'landlordInfo',
            data: result.data,
          })
          self.data.landlordInfo = result.data;
          self.data.landlordForm.id = result.data.id;
          self.data.landlordForm.ownNo = result.data.ownNo;     
          self.setData({
            landlordInfo: self.data.landlordInfo
          })
          
            switch (result.data.ownStst) {
              /**未认证 */
              case "50500101":
                wx.setNavigationBarTitle({
                  title: '身份认证'
                })
                break;
              /**待审核 */
              case "50500102":
              /**认证通过 */
              case "50500104":
                if (self.data.examine == '') {
                wx.reLaunch({
                  url: `/pages/examine/examine?ownStst=${result.data.ownStst}`
                })
                }
                break;
              case "50500103":
                http.getStoreCount({ ownNo: self.data.landlordForm.ownNo }).then(res => {
                  if (res.data.allCount > 0) {
                    wx.reLaunch({
                      url: '/pages/landlordIUser/landlordIUser'
                    })
                  } else {
                    wx.showModal({
                      title: '提示',
                      content: '是否查看《房东引导手册》',
                      cancelText: '已查看',
                      confirmText: '去查看',
                      success(res) {
                        if (res.confirm) {
                          wx.navigateTo({
                            url: `/pages/agreement/agreement?url=${encodeURIComponent('https://ms.maishe.live/applyLandlordAtt.html')}&type='user'`
                          })
                        } else if (res.cancel) {
                          wx.reLaunch({
                            url: '/pages/landlordIUser/landlordIUser'
                          })
                        }
                      }
                    })
                  }
                })
                break;
            }
        })
      },
    })
  },
  submitInfo(){
    /**提交认证 */
    if (self.checked('file') && self.checked('ownName')  && self.checked('ownLicNum') && self.checked('ownTel') && self.checked('code') && self.checked('accept')){
      wx.showLoading();
      self.setData({
        error: self.data.error
      })
      let data={
        id: self.data.landlordForm.id,
        ownLicNum: self.data.landlordForm.ownLicNum,
        ownName: self.data.landlordForm.ownName,
        ownNo: self.data.landlordForm.ownNo,
        ownTel: self.data.landlordForm.ownTel,
        code: self.data.landlordForm.code
      }
      http.updateOwnFile(self.data.landlordForm.file[0],data).then(res=>{
        wx.hideLoading();
        res=JSON.parse(res);
        if (res.code==0){
          wx.showToast({
            title: '提交认证成功',
            icon: 'success',
            duration: 2000
          })
          wx.reLaunch({
            url: `/pages/examine/examine?ownStst=${res.data.ownStst}`
          })
        }
      })
    }else{
      self.setData({
        error: self.data.error
      })
    }
  },
  watchAgreement(e){
    // 点击查看协议
    wx.navigateTo({
      url: `/pages/agreement/agreement?url=${encodeURIComponent('https://ms.maishe.live/landlordAgreement.html')}`,
    })
  },
  uploadImage(e){/**图片上传 */
    let type = e.currentTarget.dataset.type ? e.currentTarget.dataset.type:'';
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: type != "" ? [type]: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        self.data.landlordForm.file=res.tempFilePaths;
        self.setData({
          landlordForm: self.data.landlordForm
        })
      }
    })
  },
  getValus(e){
    let type = e.currentTarget.dataset.type;
    switch (type) {
      case 'ownLicNum':
        self.data.landlordForm.ownLicNum = e.detail.value;
        break;
      case 'ownName':
        self.data.landlordForm.ownName = e.detail.value;
        break;
      case 'ownTel':
        self.data.landlordForm.ownTel = e.detail.value;
        break;
      case 'code':
        self.data.landlordForm.code = e.detail.value;
        break;
    }
    self.setData({
      landlordForm: self.data.landlordForm
    })
  },
  checked(name){
    switch(name){
      case 'accept':
      self.data.error.accept = self.data.landlordForm.accept?'':'请勾选接受';
      return self.data.landlordForm.accept ? true : false;
      break;
      case 'file':
        self.data.error.file = self.data.landlordForm.file.length>0 ? '' : '请上传身份证照';
        return self.data.landlordForm.file.length > 0 ? true : false;
        break;
      case 'ownLicNum':
        if (self.data.landlordForm.ownLicNum==''){
          self.data.error.ownLicNum="请输入身份证号"
          return false;
        }
        if (!util.reg.id.test(self.data.landlordForm.ownLicNum)) {
          self.data.error.ownLicNum = "请输入正确的身份证号";
          return false;
        }
        self.data.error.ownLicNum='';
        return true;
        break;
      case 'ownName':
        self.data.error.ownName = self.data.landlordForm.ownName!='' ? '' : '请输入真实姓名';
        return self.data.landlordForm.ownName != '' ? true : false;
        break;
      case 'ownTel':
        if (self.data.landlordForm.ownTel == '') {
          self.data.error.ownTel = "请输入手机号码"
          return false;
        }
        if (!util.reg.phone.test(self.data.landlordForm.ownTel)) {
          self.data.error.ownTel = "请输入正确的手机";
          return false;
        }
        self.data.error.ownTel = '';
        return true;
        break;
      case 'code':
        self.data.error.code = self.data.landlordForm.code != '' ? '' : '请输入短信验证码';
        return self.data.landlordForm.code != '' ? true : false;
        break;
    }
  },
  getPhoneCode(e){
    if (self.checked("ownTel")){
      http.sendCode(`?phoneNo=${self.data.landlordForm.ownTel}`).then(res=>{
        self.data.code.disabled=true;
        self.setData({
          code: self.data.code
        })
        wx.showToast({
          title: '短信验证码发送成功，请注意查收',
          icon: 'success',
          duration: 2000
        })
        timeName= setInterval(()=>{
          if (self.data.times>0){
            self.data.times--;
            self.data.code.text =`${self.data.times}秒后重新发送`
            self.setData({
              code:self.data.code
            })
          }else{
            clearInterval(timeName);
            self.data.code.disabled = false;
            self.data.times=60;
            self.data.code.text = `获取验证码`
            self.setData({
              code: self.data.code
            })
          }
        },1000)
      })
      self.setData({
        error: self.data.error
      })
    }else{
      self.setData({
        error:self.data.error
      })
    }
  },
  checkboxChange(e){
    self.data.landlordForm.accept = !self.data.landlordForm.accept;
    self.setData({
      landlordForm: self.data.landlordForm
    })
  }
})